# coding=utf-8

import os
import subprocess
from sys import stdout

"""
此类是一个工具类，包括一些关于本地命令执行的通用方法
"""

class LocalCommandExecutor(object):
    """
    命令执行者
    """

    def run(self, cmd, args=None):
        """
        运行一个命令
        :return:
        """
        full_cmd = cmd
        if args:
            full_cmd = cmd + " ".join(args)

        os.system(full_cmd)

        subp = subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE)

        output = None
        while subp.poll() == None:
            output += stdout.readline()
        statuscode = subp.returncode

        # TODO 改成获取返回值的方式运行命令
        return statuscode, output

    def chomd(self, script_file):
        """
        给一个脚本赋权限
        """
        os.system("chmod +x " + script_file)
